package service.impl.v2;

import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.v2.BoardVOMapper;
import dao.v2.ConfigVOMapper;
import model.BoardVO;
import service.v2.BoardService;
@Service
public class BoardServiceImpl implements BoardService {

	@Autowired
	private BoardVOMapper dao;
	
	@Autowired
	private ConfigVOMapper configvomapper;

	public List<BoardVO> getBoardList(BoardVO boardVO) {
		List<BoardVO> voList=dao.getBoardList(boardVO);
		
		for(int i=0;i<voList.size();i++){
			BoardVO vo=new BoardVO();
			String content = voList.get(i).getContent();
			voList.get(i).setShare(vo);
			if(voList.get(i).getImg()==null ||"".equals(voList.get(i).getImg())){
				voList.get(i).getShare().setImage("");
			}else{
				voList.get(i).getShare().setImage(voList.get(i).getImg()+"?x-oss-process=image/resize,h_100");
			}
			voList.get(i).getShare().setTitle(voList.get(i).getTitle());
		    voList.get(i).getShare().setUrl(configvomapper.loadConfig("WECHAT_HOST")+"/notice_Detail?notice_id="+voList.get(i).getBoard_id());
			if(content !=null){
				Document doc = Jsoup.parse(content, "UTF-8");
				if(!"".equals(doc.text())){
					if(doc.text().indexOf(voList.get(i).getTitle())>-1){
						voList.get(i).getShare().setContent(doc.text().substring(doc.text().indexOf(voList.get(i).getTitle())+voList.get(i).getTitle().length()));
						if(voList.get(i).getShare().getContent().length()>100){
							voList.get(i).getShare().setContent(voList.get(i).getShare().getContent().substring(0, 80));
						}
						
					}else{
						if(doc.text().length()>100){
							voList.get(i).getShare().setContent(doc.text().substring(0, 80));
						}else{
							voList.get(i).getShare().setContent(doc.text());
						}
					}
				}else{
					voList.get(i).getShare().setContent("");
				}
			}else{
				voList.get(i).getShare().setContent("");
			}
			voList.get(i).getShare().setPage_index(null);
			voList.get(i).getShare().setPage_size(null);
			voList.get(i).getShare().setPage_count(null);
			voList.get(i).getShare().setCat_id(null);
			voList.get(i).getShare().setTotal(null);
			voList.get(i).setContent(null);
		}
		
		return voList;
	}

	public int getTotal(BoardVO boardVO) {
		return dao.getTotal(boardVO);
	}

	public BoardVO getBoardById(String id) {
		BoardVO boardVO =dao.getBoardById(id);
		BoardVO vo=new BoardVO();
		String content = boardVO.getContent();
		
		boardVO.setShare(vo);
		if(boardVO.getImg()==null ||"".equals(boardVO.getImg())){
			boardVO.getShare().setImage("");
		}else{
			boardVO.getShare().setImage(boardVO.getImg()+"?x-oss-process=image/resize,h_100");
		}
		boardVO.getShare().setTitle(boardVO.getTitle());
	    String url = configvomapper.loadConfig("WECHAT_HOST")+"/notice_Detail?notice_id="+id;
		boardVO.getShare().setUrl(url);
		if(content !=null){
			Document doc = Jsoup.parse(content, "UTF-8");
			if(!"".equals(doc.text())){
				if(doc.text().indexOf(boardVO.getTitle())>-1){
					boardVO.getShare().setContent(doc.text().substring(doc.text().indexOf(boardVO.getTitle())+boardVO.getTitle().length()));
					if(boardVO.getShare().getContent().length()>100){
						boardVO.getShare().setContent(boardVO.getShare().getContent().substring(0, 80));
					}
					
				}else{
					if(doc.text().length()>100){
						boardVO.getShare().setContent(doc.text().substring(0, 80));
					}else{
						boardVO.getShare().setContent(doc.text());
					}
				}
			}else{
				boardVO.getShare().setContent("");
			}
		}else{
			boardVO.getShare().setContent("");
		}
		return boardVO;
	}

	public List<BoardVO> selectBoardAll(BoardVO boardVO) {
		return dao.selectBoardAll(boardVO);
	}

	public BoardVO getDetailById(String board_id) {
		return dao.getDetailById(board_id);
	}

	public BoardVO getBuyCashHelpById(String buyCashHelpId) {
		return dao.getBuyCashHelpById(buyCashHelpId);
	}

	public void insertRead(List<BoardVO> boardList, String member_id) {
		dao.insertRead( boardList,  member_id);
	}
	
	public List<BoardVO> getBoardListUnReadAll(BoardVO boardVO, String member_id) {
		return dao.getBoardListUnReadAll( boardVO,  member_id) ;
	}
}
